﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Finder : MonoBehaviour {

	private static volatile Finder instance;   
	private static object syncRoot = new Object();   
	public static Finder Instance   
	{   
		get    
		{   
			if (instance == null)    
			{   
				lock (syncRoot)    
				{   
					if (instance == null)  {  
						Finder[] instances = FindObjectsOfType<Finder>();  
						if (instances != null){  
							for (var i = 0; i < instances.Length; i++) {  
								Destroy(instances[i].gameObject);  
							}  
						}  
						GameObject go = new GameObject("Finder");  
						instance = go.AddComponent<Finder>();  
						DontDestroyOnLoad(go);   
					}  
				}   
			}   
			return instance;   
		}   
	}  
	void Awake(){
		instance = this;
	}

	public enum FindType
	{
		explore,
		colony,
		enemy
	}
	public GameObject FindTheSystem(GameObject Start,FindType Type,int FindTimes,int playerID)//行为起点|寻找类型|数据池大小|行为玩家
	{
		float dis = Mathf.Infinity;
		GameObject[] Systems;
	
		Systems = GameObject.FindGameObjectsWithTag ("systems");
		List < GameObject >  systemTemp = new List<GameObject>();

		for (int i = 0; i < FindTimes; i++) 
		{
			GameObject TheSystem=null;
			foreach (GameObject sys in Systems) 
			{
				if (sys == Start)//不找自己
					continue;
				if (Start == null)
					return null;
				float DisNow = (sys.transform.position - Start.transform.position).magnitude;
				if (DisNow > ControlCenter.Instance.distances.MinRange)//搜索范围
					continue;
				if (DisNow < dis && JudgeType (Type,sys.GetComponent<StarSystem>(),playerID) && !systemTemp.Contains(Start) && NoCurrentEvent(playerID,sys)) 
				{				
					dis = DisNow;
					TheSystem = sys;
				}
			}
			if(TheSystem!=null)
				systemTemp.Add (TheSystem);
		}

		if (systemTemp.Count == 0)
			return null;
		else
			return systemTemp [UnityEngine.Random.Range (0, systemTemp.Count)];
	}
	bool JudgeType(FindType Type,StarSystem sys,int playerID)
	{
		switch (Type) 
		{
		case FindType.explore:
			if (sys.VisibleState [playerID-1].VisibleType == StarSystem.Visible.VisibleTypes.Unexplore)
				return true;
			break;
		case FindType.colony:
			if (sys.VisibleState [playerID-1].VisibleType == StarSystem.Visible.VisibleTypes.Explored && sys.GetComponent<StarSystem>().State_!=StarSystem.States.destroyed && sys.GetComponent<StarSystem>().Recource > 0)
				return true;
			break;
		case FindType.enemy:
			if (sys.VisibleState [playerID-1].VisibleType == StarSystem.Visible.VisibleTypes.enemy)
				return true;
			if (sys.VisibleState [playerID-1].VisibleType == StarSystem.Visible.VisibleTypes.Uncomunicate)
				return true;
			break;
		default:
			break;
		}
		return false;
	}
	bool NoCurrentEvent(int PlayerID, GameObject Tar){//防重复操作
		GameObject[] Eventss;
		Eventss = GameObject.FindGameObjectsWithTag ("events");
		foreach (GameObject eve in Eventss) 
		{
			if (eve.GetComponent<EventLine> ().Target == Tar && eve.GetComponent<EventLine> ().Player == PlayerID)
				return false;
		}
		return true;
	}

	public GameObject FindOneOfMine(int PlayerID)//随机一个自有星系
	{		
		GameObject[] Systems;
		Systems = GameObject.FindGameObjectsWithTag ("systems");
		List < GameObject >  systemTemp = new List<GameObject>();
		foreach (GameObject sys in Systems) 
		{
			if (sys.GetComponent<StarSystem> ().Owner == PlayerID) {
				systemTemp.Add (sys);
			}
		}
		if (systemTemp.Count == 0)
			return null;
		return systemTemp[Random.Range(0,systemTemp.Count)];
	}
}
